package com.longcai.piaojia.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.VenueDetailActivity;
import com.longcai.piaojia.adapter.RushedAdapter2;
import com.longcai.piaojia.adapter.VenueAdapter;
import com.longcai.piaojia.adapter.VenueRecyAdapter;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshGridView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/1/5.
 */
public class VenueFragment extends BaseFragment{

    @BoundView(R.id.venue_rv) private RecyclerView venue_rv;
    @BoundView(R.id.venue_gv) private PullToRefreshGridView pullToRefreshGridView;
    private GridView venue_gv;
    private List<String> lists = new ArrayList<>();
    private VenueAdapter adapter;
    private VenueRecyAdapter venueRecyAdapter;
    private List<String> classList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) inflater.inflate(R.layout.fragment_venue, null)));

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        venue_rv.setLayoutManager(linearLayoutManager);

        venueRecyAdapter = new VenueRecyAdapter(getActivity(), classList);
        venue_rv.setAdapter(venueRecyAdapter);

        venueRecyAdapter.setOnItemClickListener(new VenueRecyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {

                venueRecyAdapter.notifyDataSetChanged(lists, pos);
            }
        });

        pullToRefreshGridView.setScrollLoadEnabled(false);
        pullToRefreshGridView.setPullLoadEnabled(false);
        pullToRefreshGridView.setPullRefreshEnabled(true);

        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {

                pullToRefreshGridView.onPullUpRefreshComplete();
                pullToRefreshGridView.onPullDownRefreshComplete();

                setLastUpdateTime();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {

            }
        });

        venue_gv = pullToRefreshGridView.getRefreshableView();

        venue_gv.setNumColumns(2);

        venue_gv.setHorizontalSpacing(ScaleScreenHelperFactory.getInstance().getWidthHeight(-16));

        venue_gv.setVerticalScrollBarEnabled(false);


        for (int i = 0; i < 15; i++) {

            lists.add("");

        }

        venue_gv.setAdapter(adapter = new VenueAdapter(getContext(), lists));

        venue_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getContext(), VenueDetailActivity.class));

            }
        });


        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 10; i++) {

                    classList.add("蛋黄");

                }

                venueRecyAdapter.notifyDataSetChanged(lists, 0);

            }
        });

        return view;

    }


    private void setLastUpdateTime() {

        String text = new SimpleDateFormat("MM-dd HH:mm").format(new Date(System.currentTimeMillis()));

        pullToRefreshGridView.setLastUpdatedLabel(text);
    }



}
